package com.webchat.client.mall.service;

import com.webchat.common.bean.APIPageResponseBean;
import com.webchat.common.bean.APIResponseBean;
import com.webchat.common.bean.APIResponseBeanUtil;
import com.webchat.common.exception.BusinessException;
import com.webchat.domain.vo.request.mall.MallUserCartRequestVO;
import com.webchat.domain.vo.response.mall.MallUserCartListResponseVO;
import com.webchat.rmi.ugc.mall.MallUserCartClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class MallUserCartService {

    /**
     * 注入购物车RPC client
     */
    @Autowired
    private MallUserCartClient mallUserCartClient;

    /**
     * 加入购物车
     *
     * @param userCartRequest
     * @return
     */
    public Boolean addCart(MallUserCartRequestVO userCartRequest) {
        APIResponseBean<Boolean> responseBean = mallUserCartClient.addCart(userCartRequest);
        if (APIResponseBeanUtil.isOk(responseBean)) {
            return responseBean.getData();
        }
        throw new BusinessException(responseBean.getMsg());
    }

    /**
     * 删除购物车
     *
     * @param userCartRequest
     * @return
     */
    public Boolean deleteCart(MallUserCartRequestVO userCartRequest) {
        APIResponseBean<Boolean> responseBean = mallUserCartClient.deleteCart(userCartRequest);
        if (APIResponseBeanUtil.isOk(responseBean)) {
            return responseBean.getData();
        }
        throw new BusinessException(responseBean.getMsg());
    }

    /**
     * 购物车列表
     *
     * @return
     */
    public APIPageResponseBean<MallUserCartListResponseVO> page(String userId, Integer pageNo, Integer pageSize) {

        return mallUserCartClient.page(userId, pageNo, pageSize);
    }

    /**
     * 购物车sku数量修改
     *
     * @return
     */
    public Boolean updateCount(MallUserCartRequestVO userCartRequest) {
        APIResponseBean<Boolean> responseBean = mallUserCartClient.updateCount(userCartRequest);
        if (APIResponseBeanUtil.isOk(responseBean)) {
            return responseBean.getData();
        }
        throw new BusinessException(responseBean.getMsg());
    }

}
